'use strict';

angular.module('app.view.fan-data-problem')
    .config(['$stateProvider', function($stateProvider) {
        $stateProvider.state('inconsistent-CR-greed', {
            url: '/views/fan-data-problem/inconsistent-CR-greed',
            views: {
                'navigation': {
                    templateUrl: 'views/header/navigation.html',
                    controller: 'navigationCtrl'
                },
                'sidebar': {
                    templateUrl: 'views/sidebar/sidebar-fan-data-problem.html',
                    controller: 'sidebarCtrl'
                },
                'content': {
                    templateUrl: 'views/fan-data-problem/inconsistent/inconsistent-CR-greed.html',
                    controller: 'inconsistentCRGreedController'
                }
            }
        })
    }])

    .controller('inconsistentCRGreedController', ['$scope', 'Upload', 'communicationService', function($scope, Upload, communicationService) {
        $scope.inputPaths = [];
        $scope.outputPaths = [];
        $scope.addInput = function() {
            $scope.inputPaths.push({id: $scope.inputPaths.length, values: ''});
        };
        $scope.addOutput = function() {
            $scope.outputPaths.push({id: $scope.outputPaths.length, values: ''});
        };
        $scope.removeInput = function() {
            $scope.inputPaths.pop();
        };
        $scope.removeOutput = function() {
            $scope.outputPaths.pop();
        };
        $scope.addInput();
        $scope.addOutput();

        $scope.run = function() {
            var inputFilenames = '{' + $scope.inputPaths.map(function(obj) {return obj.value}).join(',') + '}';
            var outputFilenames = '{' + $scope.outputPaths.map(function(obj) {return obj.value}).join(',') + '}';
            communicationService.post('/api/fan-data-problem/inconsistent-CR-greed', {
                inputFilenames: inputFilenames,
                outputFilenames: outputFilenames,
            }).then(function(data){
                if (!(data instanceof Array)) {
                    data = [data];
                }
                var dataTables = [];
                for (var table of data) {
                    dataTables.push({
                        header: table.header,
                        allData: table.dataList,
                        dataList: table.dataList.slice(0, 100),
                        more: table.dataList.length > 100
                    });
                }
                $scope.dataTables = dataTables;

                let key = 0, value = 0;
                for(let i = 0; i < $scope.dataTables[0].allData.length; i++)
                {
                    if($scope.dataTables[0].allData[i][0] !== $scope.dataTables[0].allData[i][1])
                        key++;
                    else
                        value++;
                }
                communicationService.post('/api/fan-data-problem/statistics', {
                    operation: "update",
                    id: "2",
                    key: key,
                    value: value,
                }).then(function(){
                    console.log("inconsistent-cr-statistics: ("+key+","+value+")");
                }, function (err) {
                    console.warn("inconsistent-cr-statistics: " + err);
                });

            }, function(err) {
                console.warn(err);
            });
        };

        $scope.preview = function() {
            communicationService.get('/api/data/csv', {
                filename: $scope.inputPaths[$scope.inputPaths.length - 1].value
            }).then(function(data){
                if (!data) {
                    return;
                }
                if (!(data instanceof Array)) {
                    data = [data];
                }
                var dataTables = [];
                for (var table of data) {
                    dataTables.push({
                        header: table.header,
                        dataList: table.dataList.slice(0, 100),
                        more: table.dataList.length > 100
                    });
                }
                $scope.dataTables = dataTables;
            }, function(err) {
                console.warn(err);
            });
        };

        $scope.uploadFiles = function (file, errFiles) {
            $scope.f = file;
            $scope.errFile = errFiles && errFiles[0];
            if (file) {
                file.upload = Upload.upload({
                    url: '/api/chooseFile',
                    data: {file: file}
                });

                file.upload.then(function (response) {
                    $scope.inputPaths[$scope.inputPaths.length - 1].value = response.data.fileName;
                    $scope.preview();
                }, function (error) {
                    console.log(error);
                }, function (evt) {});
            }
        }
    }]);

